//
//  BiNaviBarView.m
//  CodoonBikeApp
//
//  Created by ChenBin on 14-3-28.
//  Copyright (c) 2014年 Nahan Cop. All rights reserved.
//

#import "BiNaviBarView.h"

@implementation BiNaviBarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        [self setClipsToBounds:NO];
        [self setClearsContextBeforeDrawing:NO];
        
        [self setBackgroundColor:UIColorFromRGB(0x1ca0ff)];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 20, self.frame.size.width-60*2, 44)];
        [_titleLabel setBackgroundColor:[UIColor clearColor]];
        [_titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setTextColor:[UIColor whiteColor]];
        [self addSubview:_titleLabel];
        [_titleLabel release];
        
        _titleImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:_titleImage];
        [_titleImage release];
        
        _leftBtn = GET_BUTTON(CGRectMake(0, 20, 44, 44), 12, NO, [UIColor whiteColor]);
        _leftBtn.backgroundColor = [UIColor clearColor];
        [self addSubview:_leftBtn];
        
        _rightBtn = GET_BUTTON(CGRectMake(MAIN_SCREEN_WIDTH-70, 20, 60, 44), 12, NO, [UIColor whiteColor]);
        _rightBtn.backgroundColor = [UIColor clearColor];
        [self addSubview:_rightBtn];
    }
    
    return self;
}

- (void)dealloc
{

    [super dealloc];
}

- (void)setLeftButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    [_leftBtn setImage:icon forState:UIControlStateNormal];
    
    NSArray *actions = [_leftBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
    
    for (NSString *strAction in actions)
    {
        [_leftBtn removeTarget:target action:NSSelectorFromString(strAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_leftBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setRightButton:(UIImage *)icon target:(id)target action:(SEL)action
{
    _rightBtn.frame = CGRectMake(MAIN_SCREEN_WIDTH-60, 20, 60, 44);
    [_rightBtn setImage:icon forState:UIControlStateNormal];
    // remove before actions
    NSArray *actions = [_rightBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
    for (NSString *strAction in actions)
    {
        [_rightBtn removeTarget:target action:NSSelectorFromString(strAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setLeftButtonTitle:(NSString *)title target:(id)target action:(SEL)action
{
    [_leftBtn setBackgroundImage:nil forState:UIControlStateNormal];
    [_leftBtn setTitle:title forState:UIControlStateNormal];
    [_leftBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [_leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    // remove before actions
    NSArray *actions = [_leftBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
    for (NSString *strAction in actions)
    {
        [_leftBtn removeTarget:target action:NSSelectorFromString(strAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_leftBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setRightButtonTitle:(NSString *)title target:(id)target action:(SEL)action
{
    [_rightBtn setBackgroundImage:nil forState:UIControlStateNormal];
    [_rightBtn setTitle:title forState:UIControlStateNormal];
    [_rightBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    // remove before actions
    NSArray *actions = [_rightBtn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
    for (NSString *strAction in actions)
    {
        [_rightBtn removeTarget:target action:NSSelectorFromString(strAction) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [_rightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTitle:(NSString*)title
{
    [self bringSubviewToFront:_titleLabel];
    _titleLabel.text = title;
}

- (void)setTitleImage:(UIImage*)image
{
    [self bringSubviewToFront:_titleImage];
    _titleImage.image = image;
    _titleImage.frame = CGRectMake((320-image.size.width)/2, 20+(self.frame.size.height-image.size.height-20)/2, image.size.width, image.size.height);
}

@end
